/**
 * Created by yangfan on 2017/1/13.
 */

define(['config/appregister',
    "i18n",
    "business/services/utils",
    "business/personMine/services/httpServices",
    "business/services/regionService",


],function(app,i18n,utils,httpServices){
    app .directive("textInput",function(){
        return {
            templateUrl:"app/business/services/comComponents/textInput.html",
            scope:{
                length:"=confLength",
                content:"=confContent"
            },
            link:function(scope,element,attr){
                scope.curLength =utils.getContentLength(scope.content);
                scope.$watch("content",function(newValue,oldValue){
                    if(newValue!= oldValue){
                        scope.curLength = utils.getContentLength(scope.content);

                    }
                });
                scope.getCurentStyle = function(type){
                    if(parseInt(scope.curLength)>parseInt(scope.length)){
                        if(type == "msg"){
                            return {
                                color:"red"
                            }
                        }
                        if(type == "border"){
                            return {
                                border:"1px solid red"
                            }
                        }

                    }else {
                        return ""
                    }
                }
            }
        }
    })
  .directive("sendMessage",function($q,$rootScope){
        return {
            restrict: 'EA',
            transclude:true,
            templateUrl:"app/business/services/comComponents/sendMessage.html",
            scope:{
                conf:"=messageConf"
            },
            link:function(scope,element,attrs){
                scope.sendMessage = function(){
                    sendMessageToPerson().then(function(){
                        scope.conf.showSuccessNotice = true ;
                        setTimeout(function(){
                            scope.$apply(function(){
                                scope.conf.showSuccessNotice = false;
                                scope.conf.isShow = false ;
                                scope.conf.content = "";
                            })
                        },500)
                    },function(){
                        scope.conf.showErrorNotice = true ;
                        setTimeout(function(){
                            scope.$apply(function(){
                                scope.conf.showErrorNotice = false;
                                scope.conf.isShow = false ;
                                scope.conf.content = "";
                            })
                        },500)
                    })

                };

                function sendMessageToPerson(){
                    console.log(scope.conf);
                    var defer = $q.defer();
                    var req = {
                        type:"POST",
                        uri:'addMessage',
                        data:{
                            byUserId:scope.conf.byUserId,
                            content:scope.conf.content
                        }

                    };
                    httpServices.messageServices(req).success(function(response){
                        defer.resolve();
                    }).error(function(){
                        defer.reject();
                    })
                    return defer.promise ;
                }


                scope.textStyle = {
                    "text-align":"center"
                };
                if(scope.conf.hasOwnProperty('align')){
                    if(scope.conf.align == 'left'){
                        scope.textStyle = {
                            "text-align":"left"
                        }
                    }
                }
                scope.cancel = function(){
                        if(typeof scope.conf.cancel == "function"){
                            scope.conf.cancel();
                        }else{
                            scope.conf.isShow =false;
                        }

                };
            }
        }
    })
})

